package cn.ffcs.unm.salary.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.unm.salary.action.bean.SalaryPeriodSchemeMainBean;
import cn.ffcs.unm.salary.manager.SalaryManager;
import cn.ffcs.unm.salary.model.SalaryPeriodScheme;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;

@Controller
@Scope("prototype")
public class SalaryPeriodSchemeMainComposer extends BasePortletComposer {

	private static final long serialVersionUID = -1398374801731627157L;

	private SalaryPeriodSchemeMainBean bean = new SalaryPeriodSchemeMainBean();
	/**
	 * 查询实体
	 */
	private SalaryPeriodScheme qrySalaryPeriodScheme;

	@SuppressWarnings("unused")
	private SalaryPeriodScheme salaryPeriodScheme;

	@Autowired
	@Qualifier("salaryManager")
	private SalaryManager salaryManager;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化
	 * 
	 * @throws Exception
	 */
	public void onCreate$salaryPeriodSchemeMainWin() throws Exception {
	}

	public void onQuery() {
		clearVariable();
		SalaryPeriodScheme objSalaryPeriodScheme = SalaryPeriodScheme.newInstance();
		PubUtil.fillPoFromBean(bean, objSalaryPeriodScheme);
		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请输入账期", "温馨提示");
			return;
		}
		int index = bean.getRadiogroup().getSelectedIndex();
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		if (index == 0) {
			String monthIdMonth = monthId.substring(0, 6);
			objSalaryPeriodScheme.setMonthId(monthIdMonth);
		}else {
			objSalaryPeriodScheme.setMonthId(monthId);
		}
		qrySalaryPeriodScheme = objSalaryPeriodScheme;
		this.bean.getSalaryPeriodSchemeListboxPaging().setActivePage(0);
		onSalaryPeriodSchemeQuery();
		salaryPeriodScheme = null;
	}

	public void onReset() {
		bean.getName().setValue(null);
		bean.getCode().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getSalaryPeriodSchemeListbox());
		this.bean.getSalaryPeriodSchemeListboxPaging().setTotalSize(1);
	}

	public void onSalaryPeriodSchemeQuery() {
		if (this.qrySalaryPeriodScheme != null) {
			int activePage = bean.getSalaryPeriodSchemeListboxPaging()
					.getActivePage();
			int pageSize = bean.getSalaryPeriodSchemeListboxPaging()
					.getPageSize();
			PageInfo pageInfo = salaryManager
					.queryPageInfoBySalaryPeriodScheme(qrySalaryPeriodScheme,
							activePage + 1, pageSize);
			ListModelList dataList = new BindingListModelList(
					pageInfo.getDataList(), true);
			bean.getSalaryPeriodSchemeListbox().setModel(dataList);
			bean.getSalaryPeriodSchemeListboxPaging().setTotalSize(
					NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qrySalaryPeriodScheme = null;
		salaryPeriodScheme = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.onSalaryPeriodSchemeQuery();
	}

}
